package com.yizn.learn.Condition;

import com.yizn.learn.Condition.util.SecondUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 查看不同状态的线程
 *
 * @author yisl
 * @create 2019-08-07 16:32
 */
public class ThreadState {
    public static void main(String[] args) {
        new Thread(new TimeWaiting(),"TimeWaitingThread").start();
        new Thread(new Waiting(),"WaitingThread").start();


        //一个Blocked获取锁一个释放锁
        new Thread(new Blocked(),"Blocked-1-Thread").start();
        new Thread(new Blocked(),"Blocked-2-Thread").start();

//        ExecutorService service = Executors.newFixedThreadPool(5);
//        service.execute(new Thread());


    }

    //该线程不断睡眠
    static class TimeWaiting implements Runnable{
        public void run() {
            while (true){
                SecondUtil.second(100);
            }
        }
    }

    //该线程在Waiting.class实例上等待
    static class Waiting implements Runnable{
        public void run() {
            while (true){
                synchronized (Waiting.class){
                    try {
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    //该线程在Blocked.class实例上上锁后不会释放锁
    static class Blocked implements Runnable{
        public void run() {

            synchronized (Waiting.class) {
                while (true) {
                    SecondUtil.second(100);
                }
            }
        }
    }
}
